package sma;

import java.io.File;
import java.io.IOException;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.apache.log4j.helpers.Loader;

import sma.gps.fich.FichPolish;
import sma.gps.fich.PolishEntity;

/**
 * Filter polish file
 * @author S. MARSOLLE
 */
public class FilterMp {

	/** Log4J */
	private static final Logger sLog = Logger.getLogger(FilterMp.class);
	
	private static String sDestPath;


	/**
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println("begin FilterMp...");
		
		long lBegin = System.currentTimeMillis();
		
		//BasicConfigurator.configure();
		PropertyConfigurator.configure(Loader.getResource("log4j.properties"));
		
		try {
			
		
		sLog.info("args.length="+ args.length);
		
		String source = args[0];
		sDestPath = args[1];
		
		File f = new File(source);
		if (f.isFile()) {
			filterOneFile(f);
		} else if (f.isDirectory()) {
			sLog.debug("lenth" +f.listFiles().length);
			File[] allFiles = f.listFiles();
			for (int i = 0; i < allFiles.length; i++) {
				File aFile = allFiles[i];
				if (aFile.getName().endsWith(".mp")) {
					filterOneFile(aFile);
				}
			}
		} else {
			sLog.error("unknow type for " + f);
		}
		
		} catch (Exception e) {
			sLog.error("error in main", e);
		}

		float lTime = (System.currentTimeMillis() - lBegin) / 1000.0f;

		
		sLog.info("...end in " + lTime + " s");
	}
	
	private static void filterOneFile(File f) throws IOException {
		File fCible = new File(sDestPath, f.getName());
		if (fCible.exists()) {
			sLog.info("file " + fCible + " already existing, skip it");
			return;
		}
		sLog.info("processing " + f.getAbsolutePath() + " to " + fCible.getAbsolutePath());
		FichPolish fp = new FichPolish(f);
		FichPolish fpCible = new FichPolish(fCible);
		fp.openRead();
		fpCible.openWrite();
		
		PolishEntity vPol;
		
		int nbEntitySource = 0;
		int nbEntityCible = 0;
		
		// first entity
		vPol = fp.readEntity();
		if ("IMG ID".equals(vPol.getFamily())) {
			// keep only 1 levels : 1 + 1 that is empty
			vPol.setValueForKey("Levels", "2");
			vPol.setValueForKey("Level0", "23");
			vPol.setValueForKey("Level1", "22");
			vPol.removeKey("Level2");
			vPol.removeKey("Level3");
			vPol.removeKey("Level4");
			// faster compilation
			String result = vPol.setValueForKey("PreProcess", "N");
			if (result == null) {
				vPol.setValueForKey("Preprocess", "N");
			}
			// compresion
			vPol.setValueForKey("TreSize", "1000");
			result = vPol.setValueForKey("RgnLimit", "512");
			if (result == null) {
				vPol.addValueForKey("RgnLimit", "512");
			}
		} else {
			sLog.error("not the requested family entity !!! " + vPol.getFamily());
		}
		fpCible.writeEntity(vPol);
		
		while ((vPol = fp.readEntity()) != null) {
			nbEntitySource++;
			if (!isFiltered(vPol)) {
				// not filtered so copy it 
				fpCible.writeEntity(vPol);
				nbEntityCible++;
			}
		}
		
		fp.closeRead();
		fpCible.closeWrite();
		sLog.info("" + nbEntityCible + " entities copied on " + nbEntitySource);
	}
	
	/**
	 * @param pPol
	 * @return true to filter it or false to keep it
	 */
	private static boolean isFiltered(PolishEntity pPol) {
		String family = pPol.getFamily();
		String type = pPol.getValueForKey("Type");
		if (pPol.getValueForKey("Data0") == null) {
			// keep only level 0
			return true;
		}
		if ("POLYLINE".equals(family)) {
			if ("0x20".equals(type)) {
				return true;
			} 
			//Major land contour (1/1) (0x22, polyline)
			if ("0x22".equals(type)) {
				return true;
			} 
			// Marine hazard (no line) (0x2b, polyline) Catalu�a
			if ("0x2b".equals(type)) {
				return true;
			} 
			
			if ("0x5".equals(type)) {
				// change grosse route -> route �troite
				pPol.setValueForKey("Type", "0x6");
				return false;
			}
			//sLog.info("keep "+ type);
			
		} else if ("POLYGON".equals(family)) {
			if ("0x13".equals(type) || "0x50".equals(type)) {
				return true;
			}
			// plantation, sport
			if ("0x4e".equals(type) || "0x17".equals(type)) {
				return true;
			}	
		} else if ("POI".equals(family)) {
			if ("0x6411".equals(type)) {
				String label = pPol.getValueForKey("Label");
				if ("PYLONE".equals(label)) {
					return true;
				}
			}
			// Label (0x2800, point) 1 lettre dans Catalu�a	
			if ("0x2800".equals(type)) {
				String label = pPol.getValueForKey("Label");
				if (label.length() <= 1) {
					return true;
				}
			}
		
			
		} else {
			sLog.info("unknown family " + family);
		}
		// by default, no filter
		return false;
	}

}
